/***************************************************************************
Name: JSView
Description: Helps you to view the source code of external js and css files
Author: Ron Beckman
Homepage: http://forum.softwareblaze.com

Copyright (C) 2008  Ron Beckman

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to:

Free Software Foundation, Inc.
51 Franklin Street
Fifth Floor
Boston, MA  02110-1301
USA
***************************************************************************/


window.onkeydown = function(event) {
	if (event.which == 13) {
		event.preventDefault();
	}
}

function jsviewTimer() {
	jsviewObjRef.load("frames['0']");
}

var jsviewObjRef = {

	getStr: function(name) {
		return document.getElementById("jsview-bundle").getString(name);
	},

	init: function() {
		//load the browser's current page in the currentPage frame
		frames[0].location = window.opener.content.location;

		//show-hide the currentPage frame
		var ml = document.getElementById('jsview-list');
		var hidden = (ml.selectedIndex == 0) ? false : true;
		this.hideFrame(hidden);

		//call timeout before dumping objects to give page time to load
		setTimeout(jsviewTimer,1000);
	},

	loadNewURL: function() {
		var urlBox = document.getElementById('urlBox');
		var url = urlBox.value.replace(/^\W*|\W*$/g,'');
		if (url.substr(0,6) != 'about:' && url.indexOf('://') == -1) {
			url = 'http://' + url;
		}
		var io = this.getIOService();
		try {
			var uri = io.newURI(url,null,null);
			urlBox.value = uri.spec;
			frames[0].location = uri.spec;
		} catch(e) {
			urlBox.value = url;
			alert(this.getStr('jsview.InvalidURI'));
		}
	},

	loadNewObject: function() {
		var obj = document.getElementById('objectsBox').value;
		this.load(obj);
	},

	load: function(objStr) {
		try {
			var obj = eval(objStr);
		} catch(e) {
			alert(this.getStr('jsview.InvalidObj'));
			return false;
		}

		var cell1,cell2,cls,objName,count = 0;
		var doc = frames[1].document;
		var table = doc.getElementById('main');
		var error = this.getStr('jsview.Error');
		var objs = this.getStr('jsview.Objects');

		//remove all table cells
		while (table.firstChild) {
			table.removeChild(table.firstChild);
		}

		//create header row
		var cells = [[objStr,
				  ['onclick','top.jsviewObjRef.load("'+objStr+'");'],
				  ['class','header']]];
		table.appendChild(this.createRow(cells));

		for (var j in obj) {
			++count;
			try {
				cell2 = obj[j];
				cls = (count % 2 == 0) ? 'even' : 'odd';
			} catch(e) {
				cell2 = error;
				cls = 'error';
			}
			objName = objStr + "['" + j + "']";
			cells = [[j,
				    ['onclick','top.jsviewObjRef.load("'+objName+'");'],
				    ['class',cls]],
				   [cell2,
				    ['onclick','top.jsviewObjRef.load("'+objName+'");'],
				    ['class',cls]]];
			table.appendChild(this.createRow(cells));
		}

		//create footer row
		objName = (objStr.indexOf('[')!=-1) ? objStr.substr(0,objStr.lastIndexOf('[')) : objStr;
		cells = [[count+' '+objs,
			    ['onclick','top.jsviewObjRef.load("'+objName+'");'],
			    ['class','footer']]];
		table.appendChild(this.createRow(cells));

		document.getElementById('objectsBox').value = objStr;
		return false;
	},

	createRow: function(cells) {
		var td;
		var doc = frames[1].document;

		var tr = doc.createElement('tr');
		for (var i=0; i<cells.length; ++i) {
			td = doc.createElement('td');
			td.appendChild(doc.createTextNode(cells[i][0]));
			if (cells.length < 2) td.setAttribute('colspan',2);
			for (var j=1; j<cells[i].length; ++j) {
				td.setAttribute(cells[i][j][0],cells[i][j][1]);
			}
			tr.appendChild(td);
		}
		return tr;
	},

	hideFrame: function(tf) {
		document.getElementById('topFrame').setAttribute('collapsed',tf);
	},

	getIOService: function(url) {
		return Components.classes["@mozilla.org/network/io-service;1"].
		getService(Components.interfaces.nsIIOService);
	}
};